/**
 * Author: Shen Yanqiu
 * Date: 2018-02-26
 * Time: 13:45
 *
 */
import modelExtend from 'dva-model-extend';
import {pageModel} from '../../common';
import {message} from 'antd';
import {delay, routerTool} from "../../../utils";

export default modelExtend(pageModel, {
  namespace: 'recruitnewStatisticsUploadTableFile',
  state: {
    uploadFiles: [],
  },
  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === routerTool.getPathByName('RecruitnewStatisticsUploadTableFile')) {
          dispatch({ type: 'firstLoad' });
        }
      });
    },
  },
  effects: {
    * firstLoad ({ payload = {} }, { put, call }) {
      yield delay(300);
    },
    * createQRCode ({ payload = {} }, { put, call, select }) {
      const { uploadFiles } = yield select(_ => _.recruitnewStatisticsUploadTableFile);
      if (!uploadFiles[0] || !uploadFiles[0].response) {
        message.error("请上传文件！");
        return;
      }
      message.success("生成二维码成功！");
      yield put({ type: 'updateUploadFiles', payload : { uploadFiles: [] } });
    },
  },
  reducers: {
    updateUploadFiles (state, { payload: { uploadFiles } }) {
      return { ...state, ...{ uploadFiles } };
    },
  },
});
